package com.bl.andr.utillib;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogUtil {
    private String TAG = this.getClass().getName();

    private static int logLevel = Log.VERBOSE;

    private static boolean isDebug = AppUtil.isDebug();

    public LogUtil() {
    }

    public LogUtil(String tag) {
        this.TAG = tag;
    }

    private String getFunctionName() {
        StackTraceElement[] sts = Thread.currentThread().getStackTrace();

        if (sts == null) {
            return null;
        }

        for (StackTraceElement st : sts) {
            if (st.isNativeMethod()) {
                continue;
            }

            if (st.getClassName().equals(Thread.class.getName())) {
                continue;
            }

            if (st.getClassName().equals(this.getClass().getName())) {
                continue;
            }

            return "[" + Thread.currentThread().getId() + ": "
                    + st.getFileName() + ":" + st.getLineNumber() + "]";
        }

        return null;
    }

    public void info(Object str) {
        if (logLevel <= Log.INFO) {
            String name = getFunctionName();
            String ls = (name == null ? str.toString() : (name + " - " + str));
            ls = addTime(ls);
            saveLog(ls);
            Log.i(TAG, ls);
        }
    }

    public void i(Object str) {
        if (isDebug) {
            info(str);
        }
    }

    public void verbose(Object str) {
        if (logLevel <= Log.VERBOSE) {
            String name = getFunctionName();
            String ls = (name == null ? str.toString() : (name + " - " + str));
            ls = addTime(ls);
            saveLog(ls);
            Log.v(TAG, ls);
        }
    }

    public void v(Object str) {
        if (isDebug) {
            verbose(str);
        }
    }

    public void warn(Object str) {
        if (logLevel <= Log.WARN) {
            String name = getFunctionName();
            String ls = (name == null ? str.toString() : (name + " - " + str));
            ls = addTime(ls);
            saveLog(ls);
            Log.w(TAG, ls);
        }
    }

    public void w(Object str) {
        if (isDebug) {
            warn(str);
        }
    }

    public void error(Object str) {
        if (logLevel <= Log.ERROR) {
            String name = getFunctionName();
            String ls = (name == null ? str.toString() : (name + " - " + str));
            ls = addTime(ls);
            saveLog(ls);
            Log.e(TAG, ls);
        }
    }

    public void error(Exception ex) {
        if (logLevel <= Log.ERROR) {
            StringBuilder sb = new StringBuilder();
            String name = getFunctionName();
            StackTraceElement[] sts = ex.getStackTrace();

            if (name != null) {
                sb.append(name).append(" - ").append(ex).append("\r\n");
            } else {
                sb.append(ex).append("\r\n");
            }

            if (sts != null && sts.length > 0) {
                for (StackTraceElement st : sts) {
                    if (st != null) {
                        sb.append("[ ").append(st.getFileName()).append(":").append(st.getLineNumber()).append(" ]\r\n");
                    }
                }
            }
            String log = addTime(sb.toString());
            saveLog(log);
            Log.e(TAG, log);
        }
    }

    public void e(Object str) {
        if (isDebug) {
            error(str);
        }
    }

    public void e(Exception e) {
        if (isDebug) {
            error(e);
        }
    }

    public void debug(Object str) {
        if (logLevel <= Log.DEBUG) {
            String name = getFunctionName();
            String ls = (name == null ? str.toString() : (name + " - " + str));
            ls = addTime(ls);
            saveLog(ls);
            Log.d(TAG, ls);
        }
    }

    public void d(Object str) {
        if (isDebug) {
            debug(str);
        }
    }

    public void syso(Object str) {
        if (isDebug) {
            saveLog(addTime(str.toString()));
//			System.out.println(str.toString());
            Log.i(TAG, addTime(str.toString()));
        }
    }

    /**
     * 字符串前加入带时分秒的时间
     *
     * @param str
     * @return
     */
    public static String addTime(String str) {
        String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.getDefault())).format(new Date(System
                .currentTimeMillis()));
        return date + " - " + str;
    }

    private void saveLog(String content) {
        if (isDebug) {
            /*content += "\n";
            File logFile = new File(Contants.LOG_PATH + "myLog"
					+ Contants.addDateTime("") + ".txt");
			try {
				File parent = logFile.getParentFile();
				if (!parent.exists()) {
					parent.mkdirs();
				}
				FileOutputStream outputStream = new FileOutputStream(logFile,
						true);
				outputStream.write(content.getBytes());
				outputStream.flush();
				outputStream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}*/
        }
    }

}